<!doctype html>



  


<html class="theme-next mist use-motion" lang="en">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta property="og:type" content="website">
<meta property="og:title" content="Ji Xiang&#39;s blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Ji Xiang&#39;s blog">
<meta property="og:locale" content="en">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Ji Xiang&#39;s blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/"/>





  <title> Ji Xiang's blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="en">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left 
   page-home 
 ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">Ji Xiang's blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle">Walk step by step and learn everyday</p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/11/18/deep-learning-md/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2018/11/18/deep-learning-md/" itemprop="url">
                  deep-learning.md
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-11-18T10:15:56+08:00">
                2018-11-18
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>背景</p>
<hr>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/05/machine-learning-md/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2018/10/05/machine-learning-md/" itemprop="url">
                  machine-learning.md
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-10-05T18:32:25+08:00">
                2018-10-05
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="机器学习主要术语"><a href="#机器学习主要术语" class="headerlink" title="机器学习主要术语"></a>机器学习主要术语</h2><h3 id="1-监督式机器学习"><a href="#1-监督式机器学习" class="headerlink" title="1.监督式机器学习"></a>1.监督式机器学习</h3><blockquote>
<p>机器学习系统通过学习如何组合输入信息来对从未见过的数据做出有用的预测。</p>
</blockquote>
<h3 id="2-标签"><a href="#2-标签" class="headerlink" title="2.标签"></a>2.标签</h3><blockquote>
<p><strong>标签</strong>是我们要预测的事物，即简单线性回归中的 y 变量。标签可以是小麦未来的价格、图片中显示的动物品种、音频剪辑中的含义或任何事物。</p>
</blockquote>
<h3 id="3-特征"><a href="#3-特征" class="headerlink" title="3.特征"></a>3.特征</h3><blockquote>
<p><strong>特征</strong>是输入变量，即简单线性回归中的 x 变量。简单的机器学习项目可能会使用单个特征，而比较复杂的机器学习项目可能会使用数百万的特征，按如下方式指定：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#123;x1, x2, ... xn&#125;</span><br></pre></td></tr></table></figure></p>
</blockquote>
<h3 id="4-样本"><a href="#4-样本" class="headerlink" title="4.样本"></a>4.样本</h3><blockquote>
<p>样本是指数据的特定实例：<strong>x</strong>。（采用粗体<strong>x</strong>表示它是一个矢量。）我们将样本分为以下两类：</p>
<ul>
<li>有标签样本</li>
<li>无标签样本</li>
</ul>
</blockquote>
<p>有标签样本同时包括特征和标签。即：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">labeled examples: &#123;features, label&#125;: (x, y)</span><br></pre></td></tr></table></figure></p>
<p>无标签样本包含特征，但不包含标签。即：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unlabeled examples: &#123;features, ?&#125;: (x, ?)</span><br></pre></td></tr></table></figure></p>
<p>在使用有标签样本训练了我们德 模型之后，我们会使用该模型来预测无标签样本的标签。</p>
<h3 id="5-模型"><a href="#5-模型" class="headerlink" title="5.模型"></a>5.模型</h3><blockquote>
<p>模型定义了特征与标签之间的关系。模型生命周期的冷两个阶段：</p>
<ul>
<li><strong>训练</strong>表示创建或学习模型。也就是说，向模型展示有标签样本，让模型逐渐学习特征与标签之间的关系。</li>
<li><strong>推断</strong>表示将训练后的模型应用于无标签的样本。也就是说，使用训练后的模型来做出有用的预测。</li>
</ul>
</blockquote>
<h3 id="6-回归于分类"><a href="#6-回归于分类" class="headerlink" title="6.回归于分类"></a>6.回归于分类</h3><ul>
<li><strong>回归</strong>模型可预测连续值</li>
<li><strong>分类</strong>模型可预测离散值</li>
</ul>
<h2 id="训练与损失"><a href="#训练与损失" class="headerlink" title="训练与损失"></a>训练与损失</h2><p>简单来说，<strong>训练</strong>模型表示通过让有标签样本来学习（确定）所有权重和偏差的理想值。在监督式学习中，机器学习算法通过以下方式构建模型：检查多个样本并尝试找出可最大限度地减少损失的模型；这一过程称为<strong>经验风险最小化</strong>。</p>
<p>损失是对糟糕预测的惩罚。也就是说，损失是一个数值，表示对于单个样本而言模型预测的准确程度。如果模型的预测完全准确，则损失为零，否则损失会较大。训练模型的目标是从所有样本中找到一组平均损失“较小”的权重和偏差。</p>
<h3 id="平均损失（L2损失）"><a href="#平均损失（L2损失）" class="headerlink" title="平均损失（L2损失）"></a>平均损失（L2损失）</h3><blockquote>
<p>(observation - prediction(<strong>x</strong>))^2 = (y - y’)2</p>
</blockquote>
<h3 id="均方损失（MSE）"><a href="#均方损失（MSE）" class="headerlink" title="均方损失（MSE）"></a>均方损失（MSE）</h3><blockquote>
<p>每个样本的平均平方损失</p>
</blockquote>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/03/07/javascript-array-reduce/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/03/07/javascript-array-reduce/" itemprop="url">
                  javascript-array-reduce
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-03-07T09:19:46+08:00">
                2017-03-07
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>Javascript中，数据类型Array有一些非常实用而强大的原生方法，接下来对主要的四种方法：forEach, map, filter, reduce做一个使用总结，由于reduce方法用的少，而且该方法本身存在难点，所以会着重介绍reduce方法。</p>
<h3 id="Array四大迭代利器"><a href="#Array四大迭代利器" class="headerlink" title="Array四大迭代利器"></a>Array四大迭代利器</h3><h4 id="1-forEach"><a href="#1-forEach" class="headerlink" title="1.forEach()"></a>1.forEach()</h4><p>forEach()方法接受一个回调函数。如果没有抛出异常，数组中的每个元素都将会执行一次该函数。   </p>
<p><strong>语法</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">arr.forEach(<span class="function"><span class="keyword">function</span> <span class="title">callback</span>(<span class="params">currentValue, index, array</span>) </span>&#123;</span><br><span class="line">    <span class="comment">//your iterator</span></span><br><span class="line">&#125;[, thisArg]);</span><br></pre></td></tr></table></figure></p>
<p><strong>参数</strong>   </p>
<table>
<thead>
<tr>
<th>callback</th>
<th>为每个元素执行的函数，接受三个参数</th>
</tr>
</thead>
<tbody>
<tr>
<td>currentValue</td>
<td>数组中当前被处理的元素</td>
</tr>
<tr>
<td>index</td>
<td>当前元素的索引</td>
</tr>
<tr>
<td>array</td>
<td>forEach()方法应用的数组</td>
</tr>
<tr>
<td>thisArg</td>
<td>(可选)执行回调时使用this（即引用对象）的值</td>
</tr>
<tr>
<td>返回值</td>
<td>undefined</td>
</tr>
</tbody>
</table>
<p><strong>注意点</strong><br>forEach()将要处理的元素在callback函数第一次执行前就已经确定，所以在forEach()开始执行后添加到数组中的元素将不会被callback处理。<br>如果现有元素的值发生了改变，传到callback里的值是forEach()取到的值。<br>在取值前被删除的元素将不会被访问。如果被访问过的元素被删除了，之后的元素会被略过（索引发生了变化）。<br>没有办法停止或打断forEach()循环，除了抛出异常。</p>
<h4 id="2-map"><a href="#2-map" class="headerlink" title="2.map()"></a>2.map()</h4><p>map()方法会返回一个由提供的回调函数作用于数组中的每个元素后产生的新数组，“纯”方法，不会污染原数组。   </p>
<p><strong>语法</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> new_array = arr.map(callback[, thisArg]);</span><br></pre></td></tr></table></figure></p>
<p><strong>参数</strong>   </p>
<table>
<thead>
<tr>
<th>callback</th>
<th>产生新数组的函数，接受三个参数</th>
</tr>
</thead>
<tbody>
<tr>
<td>currentValue</td>
<td>数组中当前被处理的元素</td>
</tr>
<tr>
<td>index</td>
<td>当前元素的索引</td>
</tr>
<tr>
<td>array</td>
<td>forEach()方法应用的数组</td>
</tr>
<tr>
<td>thisArg</td>
<td>(可选)执行回调时使用this（即引用对象）的值</td>
</tr>
<tr>
<td>返回值</td>
<td>回调函数产生的新数组</td>
</tr>
</tbody>
</table>
<p><strong>Examples</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 字符串中每个字符的字符码</span></span><br><span class="line"><span class="keyword">var</span> map = <span class="built_in">Array</span>.prototype.map;</span><br><span class="line"><span class="keyword">var</span> a = map.call(<span class="string">'string'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">c</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> c.charCodeAt(<span class="number">0</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 反转字符串</span></span><br><span class="line"><span class="keyword">var</span> str = <span class="string">'12345'</span>;</span><br><span class="line"><span class="built_in">Array</span>.prototype.map.call(str, <span class="function"><span class="keyword">function</span>(<span class="params">c</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> c;</span><br><span class="line">&#125;).reverse().join(<span class="string">''</span>);</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 字符串转整型</span></span><br><span class="line">[<span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'3'</span>].map(<span class="built_in">Number</span>);</span><br></pre></td></tr></table></figure>
<h4 id="3-filter"><a href="#3-filter" class="headerlink" title="3.filter()"></a>3.filter()</h4><p>filter()方法返回一个由提供的回调函数将数组中的每个元素过滤后组成的新数组</p>
<p><strong>语法</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> newArray = arr.filter(callback[, thisArg]);</span><br></pre></td></tr></table></figure></p>
<p><strong>参数</strong>   </p>
<table>
<thead>
<tr>
<th>callback</th>
<th>用来检测数组中的每个元素，返回true保留元素，false不保留，接受三个参数</th>
</tr>
</thead>
<tbody>
<tr>
<td>element</td>
<td>数组中当前被处理的元素</td>
</tr>
<tr>
<td>index</td>
<td>当前元素的索引</td>
</tr>
<tr>
<td>array</td>
<td>被过滤的数组</td>
</tr>
<tr>
<td>thisArg</td>
<td>(可选)执行回调时使用this（即引用对象）的值</td>
</tr>
<tr>
<td>返回值</td>
<td>满足回调函数的新数组</td>
</tr>
</tbody>
</table>
<p><strong>Examples</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 筛选掉JSON数组中不满足条件的元素</span></span><br><span class="line"><span class="keyword">var</span> arr = [</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">15</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">-1</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">0</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">3</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="number">12.2</span>&#125;,</span><br><span class="line">    &#123;&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="literal">null</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="literal">NaN</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">id</span>: <span class="string">'undefined'</span>&#125;</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> invalidEntries = <span class="number">0</span>;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isNumber</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> obj !== <span class="literal">undefined</span> &amp;&amp; <span class="keyword">typeof</span>(obj) === <span class="string">'number'</span> &amp;&amp; !<span class="built_in">isNaN</span>(obj);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">filterByID</span>(<span class="params">item</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(isNumber(item.id)) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    invalidEntries++;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arrByID = arr.filter(filterByID);</span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 数组元素查询</span></span><br><span class="line"><span class="keyword">var</span> fruits = [<span class="string">'apple'</span>, <span class="string">'banana'</span>, <span class="string">'grapes'</span>, <span class="string">'mango'</span>, <span class="string">'orange'</span>];</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">filterItems</span>(<span class="params">query</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> query.filter(<span class="function"><span class="keyword">function</span>(<span class="params">el</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> el.toLowerCase().indexOf(query.toLowerCase()) &gt; <span class="number">-1</span>;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="4-reduce"><a href="#4-reduce" class="headerlink" title="4.reduce()"></a>4.reduce()</h4><p><strong>官方定义：</strong>The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.   </p>
<p><strong>语法</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">arr.reduce(callback, [initialValue]);</span><br></pre></td></tr></table></figure></p>
<p><strong>参数</strong>   </p>
<table>
<thead>
<tr>
<th>callback</th>
<th>作用于数组中每个元素的方法，接受四个参数</th>
</tr>
</thead>
<tbody>
<tr>
<td>accumulator</td>
<td>作为每次迭代的初始值，同时也作为上一次迭代的结果值，如果是第一次执行callback，并且给了initialValue，则为initialValue的值，否则为数组第一个元素的值</td>
</tr>
<tr>
<td>currentValue</td>
<td>数组中当前被处理的元素</td>
</tr>
<tr>
<td>currentIndex</td>
<td>当前元素的索引，如果给了initialValue，索引从0开始，否则从1开始</td>
</tr>
<tr>
<td>array</td>
<td>reduce的数组</td>
</tr>
<tr>
<td>initialValue</td>
<td>(可选)callback第一次调用时使用的第一个值，即初始值</td>
</tr>
<tr>
<td>返回值</td>
<td>返回最终的结果</td>
</tr>
</tbody>
</table>
<p><strong>描述</strong><br>reduce()执行一个接受四个参数的回调函数。回调函数第一次调用时，如果有initialValue，accumulator的值为initialValue，currentValue的值为数组第一个元素的值。如果没有设置initialValue，accumulator的值为数组第一个元素的值，currentValue的值为数组第二个元素的值。<br><strong>Note</strong> 如果没有提供initialValue，reduce将会从索引1开始执行callback，略过第一个索引。如果提供了，从索引0开始。   </p>
<p>如果数组是空的，并且没有提供initialValue，将会抛出<strong>TyoeError</strong>。如果数组只有一个元素并且没有提供initialValue，或者数组是空的但提供了initialValue，reduce将会返回这一个孤独的值而不调用callback。   </p>
<p><strong>Examples</strong><br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 数组扁平化</span></span><br><span class="line"><span class="keyword">var</span> flattened = [[<span class="number">0</span>, <span class="number">1</span>], [<span class="number">2</span>, <span class="number">3</span>], [<span class="number">4</span>, <span class="number">5</span>]].reduce(</span><br><span class="line">  <span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a.concat(b);</span><br><span class="line">  &#125;,</span><br><span class="line">  []</span><br><span class="line">);</span><br><span class="line"><span class="comment">// flattened is [0, 1, 2, 3, 4, 5]</span></span><br></pre></td></tr></table></figure></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 统计数组中的值的种类</span></span><br><span class="line"><span class="keyword">var</span> names = [<span class="string">'Alice'</span>, <span class="string">'Bob'</span>, <span class="string">'Tiff'</span>, <span class="string">'Bruce'</span>, <span class="string">'Alice'</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> countedNames = names.reduce(<span class="function"><span class="keyword">function</span> (<span class="params">allNames, name</span>) </span>&#123; </span><br><span class="line">  <span class="keyword">if</span> (name <span class="keyword">in</span> allNames) &#123;</span><br><span class="line">    allNames[name]++;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span> &#123;</span><br><span class="line">    allNames[name] = <span class="number">1</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> allNames;</span><br><span class="line">&#125;, &#123;&#125;);</span><br><span class="line"><span class="comment">// countedNames is:</span></span><br><span class="line"><span class="comment">// &#123; 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 &#125;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 取出数组中的所有对象里的某一个属性的值，并返回一个包含这些值的新数组</span></span><br><span class="line"><span class="comment">// friends - an array of objects </span></span><br><span class="line"><span class="comment">// where object field "books" - list of favorite books </span></span><br><span class="line"><span class="keyword">var</span> friends = [&#123;</span><br><span class="line">  name: <span class="string">'Anna'</span>,</span><br><span class="line">  books: [<span class="string">'Bible'</span>, <span class="string">'Harry Potter'</span>],</span><br><span class="line">  age: <span class="number">21</span></span><br><span class="line">&#125;, &#123;</span><br><span class="line">  name: <span class="string">'Bob'</span>,</span><br><span class="line">  books: [<span class="string">'War and peace'</span>, <span class="string">'Romeo and Juliet'</span>],</span><br><span class="line">  age: <span class="number">26</span></span><br><span class="line">&#125;, &#123;</span><br><span class="line">  name: <span class="string">'Alice'</span>,</span><br><span class="line">  books: [<span class="string">'The Lord of the Rings'</span>, <span class="string">'The Shining'</span>],</span><br><span class="line">  age: <span class="number">18</span></span><br><span class="line">&#125;];</span><br><span class="line"></span><br><span class="line"><span class="comment">// allbooks - list which will contain all friends books +  </span></span><br><span class="line"><span class="comment">// additional list contained in initialValue</span></span><br><span class="line"><span class="keyword">var</span> allbooks = friends.reduce(<span class="function"><span class="keyword">function</span>(<span class="params">prev, curr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> [...prev, ...curr.books];</span><br><span class="line">&#125;, [<span class="string">'Alphabet'</span>]);</span><br><span class="line"></span><br><span class="line"><span class="comment">// allbooks = [</span></span><br><span class="line"><span class="comment">//   'Alphabet', 'Bible', 'Harry Potter', 'War and peace', </span></span><br><span class="line"><span class="comment">//   'Romeo and Juliet', 'The Lord of the Rings',</span></span><br><span class="line"><span class="comment">//   'The Shining'</span></span><br><span class="line"><span class="comment">// ]</span></span><br></pre></td></tr></table></figure>
          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/03/04/svg-viewport-viewbox/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/03/04/svg-viewport-viewbox/" itemprop="url">
                  svg_viewport&viewbox
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-03-04T22:27:04+08:00">
                2017-03-04
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>学习svg，除了掌握基本的图形元素，更要清楚svg元素的大小与svg内部图形大小的关系，svg图像的 viewport 和 view box 共同设置了图像可见部分的尺寸。现在我们一起学习下 viewport 和 view box 的知识，也许清不清楚这些知识对svg元素的使用不会产生多大的影响，但涉及到复杂图形的时候，了解这些知识会有助于我们更快速、更精准的控制元素，并且减轻维护成本。</p>
<h3 id="1-The-Viewport-and-View-Box"><a href="#1-The-Viewport-and-View-Box" class="headerlink" title="1. The Viewport and View Box"></a>1. The Viewport and View Box</h3><p><strong>viewport</strong> 是创建svg元素时给它设置的 width 和 height，决定了svg图像的可视区域。逻辑上svg图像可以无限大，但每次只有特定的区域是可视的，这个可视区域就是 viewport。你可以通过svg元素的 width 和 height 属性来指定 viewport 的大小：如</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">width</span>=<span class="string">"500"</span> <span class="attr">height</span>=<span class="string">"300"</span>&gt;</span><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>这个例子定义了一个宽500单位、高300单位的viewport。   </p>
<p><strong>view box</strong> 是 svg的一个属性，设置了内部图形的坐标系大小。   </p>
<p>更直观的解释是：viewport 就像是显示器，有固定的宽高，你只能在显示器屏幕的范围内观看影像，view box 设置了显示器里的影像通过何种比例显示出来。就像你看到一个大海，它也许只占了显示器里大小的1/4，但它实际的大小却是显示器的n倍。viewport 和 view box 指定了两个坐标系，内部的图形会根据这两个坐标系做适当的缩放来适应性的显示出来。</p>
<h3 id="2-svg坐标系单位"><a href="#2-svg坐标系单位" class="headerlink" title="2. svg坐标系单位"></a>2. svg坐标系单位</h3><p>如果你没有指定 width 和 height 属性的单位，那么单位会被默认为 px。你还可以使用 px 以外的单位，</p>
<table>
<thead>
<tr>
<th>单位</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>em</td>
<td>相对单位：相对于父元素字体大小</td>
</tr>
<tr>
<td>ex</td>
<td>相对单位：相对于字符 x 的高度(很少使用)</td>
</tr>
<tr>
<td>px</td>
<td>绝对单位，但相对于设备分辨率：像素</td>
</tr>
<tr>
<td>pt</td>
<td>绝对单位：点(1／72英寸)</td>
</tr>
<tr>
<td>pc</td>
<td>绝对单位：(1／6英寸)</td>
</tr>
<tr>
<td>cm</td>
<td>绝对单位：厘米</td>
</tr>
<tr>
<td>mm</td>
<td>绝对单位：毫米</td>
</tr>
<tr>
<td>in</td>
<td>绝对单位：英寸</td>
</tr>
</tbody>
</table>
<p>给svg元素设置的单位只会影响svg元素的大小(viewport)。svg图像里显示的svg图形元素的大小取决于你给每个图形设置的单位。如果没有指定单位，默认使用 px。</p>
<p>下面展示一个svg元素与svg图像里的图形设置不同单位的例子：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">width</span>=<span class="string">"10cm"</span> <span class="attr">height</span>=<span class="string">"10cm"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">rect</span> <span class="attr">x</span>=<span class="string">"50"</span> <span class="attr">y</span>=<span class="string">"100"</span> <span class="attr">width</span>=<span class="string">"50"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">style</span>=<span class="string">"stroke: #000000; fill: none;"</span>&gt;</span><span class="tag">&lt;/<span class="name">rect</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">rect</span> <span class="attr">x</span>=<span class="string">"100"</span> <span class="attr">y</span>=<span class="string">"100"</span> <span class="attr">width</span>=<span class="string">"50mm"</span> <span class="attr">height</span>=<span class="string">"50mm"</span> <span class="attr">style</span>=<span class="string">"stroke: #000000; fill: none;"</span>&gt;</span><span class="tag">&lt;/<span class="name">rect</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>svg图像单单位是cm，两个rect元素有自己的单位：第一个没有指明单位，默认px；第二个使用了mm。结果如下：</p>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488696584964" alt="pic_1"></p>
<h3 id="3-The-View-Box"><a href="#3-The-View-Box" class="headerlink" title="3. The View Box"></a>3. The View Box</h3><p>你可以通过 viewBox 属性重新定义没有单位的坐标。注：view box属性不要带单位。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">width</span>=<span class="string">"500"</span> <span class="attr">height</span>=<span class="string">"200"</span> <span class="attr">viewBox</span>=<span class="string">"0 0 50 20"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">rect</span> <span class="attr">x</span>=<span class="string">"20"</span> <span class="attr">y</span>=<span class="string">"10"</span> <span class="attr">width</span>=<span class="string">"10"</span> <span class="attr">height</span>=<span class="string">"5"</span> <span class="attr">style</span>=<span class="string">"stroke: #000000; fill: none;"</span>&gt;</span><span class="tag">&lt;/<span class="name">rect</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>这个例子中我们创建了一个宽500px高200px的svg元素。viewBox属性有四个值 x y width height，这些值定义了svg元素的视区，x 和 y是视区的左上角坐标，width 和 height是视区的宽高。在这个例子中，视区从(0, 0)开始，宽高分别是50和20，这意味着宽500px、高200px的svg元素在内部使用的坐标系是从(0, 0)开始，到(50, 20)。换句话说，svg元素内部坐标系中的1单位相当于宽度500/50=10px、高度200/20=10px。这就是为什么x轴偏移20单位、y轴偏移10单位的矩形实际位置却是偏移到了(200px, 100px)处。因为内部坐标系中1单位相当于10px。</p>
<p>结果为：</p>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488691161226" alt="pic_2"></p>
<h3 id="4-Preserving-Aspect-Ratio"><a href="#4-Preserving-Aspect-Ratio" class="headerlink" title="4. Preserving Aspect Ratio"></a>4. Preserving Aspect Ratio</h3><p>如果 viewport 和 view box 没有使用相同的长宽比(repect ratio)，你需要指定浏览器如何显示svg图像，这时你需要svg元素的preserveAspectRatio属性。   </p>
<p>preserveAspectRatio属性由两个被空格分隔的值组成。第一个值设置view box在viewport中如何对齐，这个值由两部分组成；第二个值设置如何保留长宽比（如果需要的话）。   </p>
<p>第一个值由两部分组成：x轴的对其方式和y轴的对齐方式，下面是这两个值的列表：</p>
<table>
<thead>
<tr>
<th>值</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>xMin</td>
<td>将 view box 和 viewport 左边对齐</td>
</tr>
<tr>
<td>xMid</td>
<td>将 view box 和 viewport x轴中心对齐</td>
</tr>
<tr>
<td>xMax</td>
<td>将 view box 和 viewport 右边对齐</td>
</tr>
<tr>
<td>yMin</td>
<td>将 view box 和 viewport 上边对齐</td>
</tr>
<tr>
<td>yMid</td>
<td>将 view box 和 viewport y轴中心对齐</td>
</tr>
<tr>
<td>yMax</td>
<td>将 view box 和 viewport 下边对齐</td>
</tr>
</tbody>
</table>
<p>这两个部分可以通过驼峰命名的方式组合起来：xMinYMin。   </p>
<p>第二个值有三个值：</p>
<table>
<thead>
<tr>
<th>值</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>meet</td>
<td>保留长宽比并缩放 view box 以适应 viewport</td>
</tr>
<tr>
<td>slice</td>
<td>保留长宽比并把超出 viewport 的部分裁剪掉</td>
</tr>
<tr>
<td>none</td>
<td>不保留长宽比，缩放图像以将 view box 完全置入 viewport。长宽比被破坏</td>
</tr>
</tbody>
</table>
<p>这两个值之间需要有一个空格：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">preserveAspectRatio=&quot;xMidYMid meet&quot;</span><br><span class="line">preserveAspectRatio=&quot;xMinYMin slice&quot;</span><br></pre></td></tr></table></figure></p>
<p>接下来我们来看看不同的 preserveAspectRatio 属性值带来的效果。</p>
<h5 id="1"><a href="#1" class="headerlink" title="(1)."></a>(1).</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">width</span>=<span class="string">"500"</span> <span class="attr">height</span>=<span class="string">"75"</span> <span class="attr">viewBox</span>=<span class="string">"0 0 250 75"</span> <span class="attr">style</span>=<span class="string">"border: 1px solid #cccccc;"</span>&gt;</span></span><br><span class="line">    &lt;rect x="1" y="1" width="50" height="50" style="stroke: #000000; fill: none;"&lt;/rect&gt;</span><br><span class="line"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488693398271" alt="pic_3"></p>
<p>这种不设置 preserveAspectRatio 的情况与 xMidYMid meet 的效果相同，由此可推测浏览器会默认让图像以最合适、最舒服的方式显示出来。</p>
<h5 id="2"><a href="#2" class="headerlink" title="(2)."></a>(2).</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">width</span>=<span class="string">"500"</span> <span class="attr">height</span>=<span class="string">"75"</span> <span class="attr">viewBox</span>=<span class="string">"0 0 250 75"</span> <span class="attr">preserveAspectRatio</span>=<span class="string">"xMinYMin meet"</span> <span class="attr">style</span>=<span class="string">"border: 1px solid #cccccc;"</span>&gt;</span></span><br><span class="line">    &lt;rect x="1" y="1" width="50" height="50" style="stroke: #000000; fill: none;"&lt;/rect&gt;</span><br><span class="line"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488693927989" alt="pic_4"><br>这个例子中设置了 preserveAspectRatio 为 xMinYMin meet，这会确保长宽比是保留了的，view box 会调整大小来适应 viewport。所以，view box 根据两个长宽比中(500/250=2, 75/75=1)较小的比例进行缩放(width: 50*1=50, height: 50*1=50)，xMinYMin 使图形左上对齐。</p>
<h5 id="3"><a href="#3" class="headerlink" title="(3)."></a>(3).</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">width</span>=<span class="string">"500"</span> <span class="attr">height</span>=<span class="string">"75"</span> <span class="attr">viewBox</span>=<span class="string">"0 0 250 75"</span> <span class="attr">preserveAspectRatio</span>=<span class="string">"xMinYMin slice"</span> <span class="attr">style</span>=<span class="string">"border: 1px solid #cccccc;"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">rect</span> <span class="attr">x</span>=<span class="string">"1"</span> <span class="attr">y</span>=<span class="string">"1"</span> <span class="attr">width</span>=<span class="string">"50"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">style</span>=<span class="string">"stroke: #000000; fill: none;"</span>&gt;</span><span class="tag">&lt;/<span class="name">rect</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488694387507" alt="pic_5"><br>这个例子中设置了 preserveAspectRatio 为 xMinYMin slice，同样保留了长宽比，但 view box 根据两个比例中(500/250=2, 75/75=1)较大的比例进行缩放(width: 50*2=100, height: 50*2=100)，结果超过了 viewport 的大小，所以 slice 掉超出的部分。</p>
<h5 id="4"><a href="#4" class="headerlink" title="(4)."></a>(4).</h5><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">width</span>=<span class="string">"500"</span> <span class="attr">height</span>=<span class="string">"75"</span> <span class="attr">viewBox</span>=<span class="string">"0 0 250 75"</span> <span class="attr">preserveAspectRatio</span>=<span class="string">"none"</span> <span class="attr">style</span>=<span class="string">"border: 1px solid #cccccc;"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">rect</span> <span class="attr">x</span>=<span class="string">"1"</span> <span class="attr">y</span>=<span class="string">"1"</span> <span class="attr">width</span>=<span class="string">"50"</span> <span class="attr">height</span>=<span class="string">"50"</span> <span class="attr">style</span>=<span class="string">"stroke: #000000; fill:none;"</span>&gt;</span><span class="tag">&lt;/<span class="name">rect</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488694788943" alt="pic_6"><br>这个例子中设置了 preserveAspectRatio 为 none，view box 将会填满整个 viewport，从而使图像失真，因为x轴和y轴的长宽比不同(x轴 2:1，y轴 1:1)。</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/02/18/css-centering/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/02/18/css-centering/" itemprop="url">
                  css-centering
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-02-18T20:26:34+08:00">
                2017-02-18
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>CSS居中总结：CSS居中是任何一个前端开发者必备的基础技能，实现居中不算很有技术难度的事情，但能实现居中的方式实在是太多了，这里对各种居中方法做一个总结，毕竟温故而知新嘛！</p>
<h3 id="一-水平居中"><a href="#一-水平居中" class="headerlink" title="一.水平居中"></a>一.水平居中</h3><h5 id="1-inline-or-line-元素"><a href="#1-inline-or-line-元素" class="headerlink" title="1.inline or line-* 元素"></a>1.inline or line-* 元素</h5><p>在块级父元素内的行内元素可以通过设置 <strong>text-align:center</strong> 来进行水平居中。</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">10px</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.parent</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">50px</span>;</span><br><span class="line">    <span class="attribute">text-align</span>: center;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000000</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"parent"</span>&gt;</span>水平居中<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488720138086" alt="pic_1"></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">nav</span> &#123;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">10px</span>;</span><br><span class="line">    <span class="attribute">text-align</span>: center;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">10px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000000</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-tag">nav</span> <span class="selector-tag">a</span> &#123;</span><br><span class="line">    <span class="attribute">text-decoration</span>: none;</span><br><span class="line">    <span class="attribute">background</span>: <span class="number">#333</span>;</span><br><span class="line">    <span class="attribute">border-radius</span>: <span class="number">5px</span>;</span><br><span class="line">    <span class="attribute">color</span>: white;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">3px</span> <span class="number">8px</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">role</span>=<span class="string">'navigation'</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#0"</span>&gt;</span>One<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#0"</span>&gt;</span>Two<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#0"</span>&gt;</span>Three<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#0"</span>&gt;</span>Four<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488720502935" alt="pic_2"></p>
<h5 id="2-块级元素"><a href="#2-块级元素" class="headerlink" title="2.块级元素"></a>2.块级元素</h5><p>如果该块级元素有具体的 width，可以通过设置块级元素的 <strong>margin-left</strong> 和 <strong>margin-right</strong> 的值为 <strong>auto</strong> 来居中。<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.main</span> &#123;</span><br><span class="line">    <span class="attribute">background</span>: white;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">20px</span> <span class="number">0</span>;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">10px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.center</span> &#123;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">0</span> auto;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">20px</span>;</span><br><span class="line">    <span class="attribute">color</span>: <span class="number">#000000</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000000</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"main"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"center"</span>&gt;</span></span><br><span class="line">    I'm a block level element and am centered.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488721052778" alt="pic_3"></p>
<h5 id="3-多个块级元素"><a href="#3-多个块级元素" class="headerlink" title="3.多个块级元素"></a>3.多个块级元素</h5><p>如果你需要把两个或两个以上的块级元素在一行水平居中，你需要把这些块级元素的 <strong>display</strong> 设置为 <strong>inline-block</strong>，或者使用 <strong>flex</strong> 布局。</p>
<p>设置 display，<strong>需设置块级元素的宽度</strong><br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.inline-block-center</span> &#123;</span><br><span class="line">    <span class="attribute">text-align</span>: center;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.inline-block-center</span> <span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">max-width</span>: <span class="number">125px</span>;</span><br><span class="line">    <span class="attribute">display</span>: inline-block;</span><br><span class="line">    <span class="attribute">text-align</span>: left;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000000</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"inline-block-center"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    I'm an element that is block-like with my siblings and we're centered in a row.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    I'm an element that is block-like with my siblings and we're centered in a row.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488722458999" alt="pic_4"></p>
<p>使用 flex 布局<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.flex-center</span> &#123;</span><br><span class="line">    <span class="attribute">display</span>: flex;</span><br><span class="line">    <span class="attribute">justify-content</span>: center;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.flex-center</span> <span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000000</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"flex-center"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    I'm an element that is block-like with my siblings and we're centered in a row.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    I'm an element that is block-like with my siblings and we're centered in a row.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488722803508" alt="pic_5"></p>
<h3 id="二-垂直居中"><a href="#二-垂直居中" class="headerlink" title="二.垂直居中"></a>二.垂直居中</h3><h5 id="1-inline-or-line-元素-1"><a href="#1-inline-or-line-元素-1" class="headerlink" title="1.inline or line-* 元素"></a>1.inline or line-* 元素</h5><p>单行<br>(1).设置相同的 padding-top 和 padding-bottom 值。<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.single</span> &#123;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">20px</span> <span class="number">0</span>;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">50px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.single</span> <span class="selector-tag">a</span> &#123;</span><br><span class="line">    <span class="attribute">background</span>: black;</span><br><span class="line">    <span class="attribute">color</span>: white;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">40px</span> <span class="number">30px</span>;</span><br><span class="line">    <span class="attribute">text-decoration</span>: none;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000000</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"single"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#0"</span>&gt;</span>We're<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#0"</span>&gt;</span>Centered<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#0"</span>&gt;</span>Bits of<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">"#0"</span>&gt;</span><span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488723144293" alt="pic_6"></p>
<p>(2).设置 line-height 与 父元素 height 相等<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.single-lineheight</span> &#123;</span><br><span class="line">    <span class="attribute">background</span>: white;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">20px</span> <span class="number">0</span>;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">40px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.single-lineheight</span> <span class="selector-tag">div</span> &#123;</span><br><span class="line">    <span class="attribute">color</span>: black;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">    <span class="attribute">line-height</span>: <span class="number">100px</span>;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">20px</span>;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">50%</span>;</span><br><span class="line">    <span class="attribute">white-space</span>: nowrap;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000000</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"single-lineheight"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">    I'm a centered line.</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488723707299" alt="pic_7"></p>
<p>多行<br>flex 布局<br><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.flex-center-more</span> &#123;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">240px</span>;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">20px</span>;</span><br><span class="line">    <span class="attribute">color</span>: <span class="number">#000000</span>;</span><br><span class="line">    <span class="attribute">display</span>: flex;</span><br><span class="line">    <span class="attribute">flex-direction</span>: column;</span><br><span class="line">    <span class="attribute">justify-content</span>: center;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">200px</span>;</span><br><span class="line">    <span class="attribute">overflow</span>: auto;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">&#125;</span><br><span class="line"><span class="selector-class">.flex-center-more</span> <span class="selector-tag">p</span> &#123;</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">0</span>;</span><br><span class="line">    <span class="attribute">padding</span>: <span class="number">20px</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">1px</span> solid <span class="number">#000000</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"flex-center-more"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>I'm vertically centered multiple lines of text in a flexbox container.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><img src="http://o7bp9e1ec.bkt.clouddn.com/1488724047544" alt="pic_8"></p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/02/12/javascript-methods/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/02/12/javascript-methods/" itemprop="url">
                  javascript-methods
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-02-12T21:22:11+08:00">
                2017-02-12
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="Javascript-包含了少量用在标准类型上的标准方法。"><a href="#Javascript-包含了少量用在标准类型上的标准方法。" class="headerlink" title="Javascript 包含了少量用在标准类型上的标准方法。"></a>Javascript 包含了少量用在标准类型上的标准方法。</h2><h2 id="Array"><a href="#Array" class="headerlink" title="Array"></a>Array</h2><h3 id="array-concat-item…"><a href="#array-concat-item…" class="headerlink" title="array.concat(item…)"></a>array.concat(item…)</h3><p>concat方法返回一个新数组，它包含array的浅复制，，并将一个或多个参数 item 附加在其后。如果参数是一个数组，那么它的每个元素会被分别添加。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>];</span><br><span class="line"><span class="keyword">var</span> b = [<span class="string">'x'</span>, <span class="string">'y'</span>, <span class="string">'z'</span>];</span><br><span class="line"><span class="keyword">var</span> c = a.concat(b, <span class="literal">true</span>);</span><br><span class="line"><span class="comment">// c 是 ['a', 'b', 'c', 'x', 'y', 'z', true]</span></span><br></pre></td></tr></table></figure></p>
<h3 id="array-join-separator"><a href="#array-join-separator" class="headerlink" title="array.join(separator)"></a>array.join(separator)</h3><p>join方法把一个 array 构造成一个字符串。它将 array 中的每个元素构造成一个字符串，并且用一个 separator 作为分隔符把它们连接在一起。默认的 separator 是 ‘,’。为了实现无间隔的连接，我们可以使用空字符串作为 separator。<br>如果你想把大量的片段组装成一个字符串，把这些片段放到一个数组中并用 join 方法连接它们通常比用 + 元素运算符连接这些片段要快。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.join(<span class="string">''</span>);</span><br><span class="line"><span class="comment">// b 是 'abc';</span></span><br></pre></td></tr></table></figure></p>
<h3 id="array-pop"><a href="#array-pop" class="headerlink" title="array.pop()"></a>array.pop()</h3><p>pop 和 push 方法使数组 array 像堆栈一样工作。pop 移除 array 中的最后一个元素并返回该元素。如果 array 是空的，它会返回 undefined。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.pop();</span><br><span class="line"><span class="comment">// a 是 ['a', 'b'], b 是 'c'</span></span><br></pre></td></tr></table></figure></p>
<h3 id="array-push-item…"><a href="#array-push-item…" class="headerlink" title="array.push(item…)"></a>array.push(item…)</h3><p>push 方法将一个或者多个参数 item 附加到一个数组的尾部。不像 concat 方法那样，它会修改数组 array，如果参数 item 是一个数组，它会将参数数组作为整个添加到数组中。它返回这个数组 array 的新长度值。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var a = [&apos;a&apos;, &apos;b&apos;, &apos;c&apos;];</span><br><span class="line">var b = [&apos;x&apos;, &apos;y&apos;, &apos;z&apos;];</span><br><span class="line">var c = a.push(b, true);</span><br><span class="line">// a 是[&apos;a&apos;, &apos;b&apos;, &apos;c&apos;, [&apos;x&apos;, &apos;y&apos;, &apos;z&apos;], true], c 是 5</span><br></pre></td></tr></table></figure></p>
<h3 id="array-reverse"><a href="#array-reverse" class="headerlink" title="array.reverse()"></a>array.reverse()</h3><p>reverse 方法反转数组 array 中元素的顺序。它返回当前的 array<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.reverse();</span><br><span class="line"><span class="comment">// a 和 b 都是 ['c', 'b', 'a']</span></span><br></pre></td></tr></table></figure></p>
<h3 id="array-shift"><a href="#array-shift" class="headerlink" title="array.shift()"></a>array.shift()</h3><p>shift 方法移除数组 array 中的第一个元素。如果这个数组是空的，它会返回 undefined。shift 通常比 pop 慢得多。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.shift();</span><br><span class="line"><span class="comment">// a 是 ['b', 'c'], b 是 'a'</span></span><br></pre></td></tr></table></figure></p>
<h3 id="array-slice-start-end"><a href="#array-slice-start-end" class="headerlink" title="array.slice(start, end)"></a>array.slice(start, end)</h3><p>slice 对 array 中的一段做浅复制。第一个被复制的元素是 array[start]，它将一直复制到 array[end]为止。end 参数是可选的，并且默认值是该数组的长度。如果两个参数中的任何一个是负数，array.length 将和它们相加起来试图使它们成为非负数。如果 start 大于等于 array.length，得到的结果将是一个新的空数组。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.slice(<span class="number">0</span>, <span class="number">1</span>); <span class="comment">// b 是 'a'</span></span><br></pre></td></tr></table></figure></p>
<h3 id="array-sort"><a href="#array-sort" class="headerlink" title="array.sort()"></a>array.sort()</h3><p>sort 方法对 array 进行适当的排序，它不能正确给一组数字排序，因为该方法假定排序的元素是字符串，所以会把数字转换为字符串，从而数字数组排序会得到一个“错误”的结果。但可以使用自己的比较函数。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">10</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">7</span>];</span><br><span class="line"><span class="comment">// 从小到大排序</span></span><br><span class="line">a.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a - b;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// a 是 [2, 4, 7, 10]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 从大到小排序</span></span><br><span class="line">a.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> b - a;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// a 是 [10, 7, 4, 2]</span></span><br></pre></td></tr></table></figure></p>
<p>上面的方法只能给数字排序，如果给任意简单值数组排序，则必须做更多的工作<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="string">'aa'</span>, <span class="string">'bb'</span>, <span class="string">'d'</span>, <span class="number">23</span>, <span class="number">12</span>, <span class="number">1</span>, <span class="number">45</span>];</span><br><span class="line">a.sort(<span class="function"><span class="keyword">function</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(a === b) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">typeof</span> a === <span class="keyword">typeof</span> b) &#123;</span><br><span class="line">        <span class="keyword">return</span> a &lt; b ? <span class="number">-1</span> : <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">typeof</span> a &lt; <span class="keyword">typeof</span> b ? <span class="number">-1</span> : <span class="number">1</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// a 是 [1, 12, 23, 45, "aa", "bb", "d"]</span></span><br></pre></td></tr></table></figure></p>
<p>给对象数组排序<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// by 函数接受一个成员名字符串作为参数</span></span><br><span class="line"><span class="comment">// 并返回一个可以用来对包含该成员的对象数组进行排序的比较函数</span></span><br><span class="line"><span class="keyword">var</span> by = <span class="function"><span class="keyword">function</span>(<span class="params">name</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">o, p</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> a, b;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">typeof</span> o === <span class="string">'object'</span> &amp;&amp; <span class="keyword">typeof</span> p === <span class="string">'object'</span> &amp;&amp; o &amp;&amp; p) &#123;</span><br><span class="line">            a = o[name];</span><br><span class="line">            b = p[name];</span><br><span class="line">            <span class="keyword">if</span>(a === b) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">typeof</span> a === <span class="keyword">typeof</span> b) &#123;</span><br><span class="line">                <span class="keyword">return</span> a &lt; b ? <span class="number">-1</span> : <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">typeof</span> a &lt; <span class="keyword">typeof</span> b ? <span class="number">-1</span> : <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">throw</span> &#123;</span><br><span class="line">                name: <span class="string">'Error'</span>,</span><br><span class="line">                message: <span class="string">'Expected an object when sorting by '</span> + name</span><br><span class="line">            &#125;;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> s = [</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Joe'</span>, <span class="attr">last</span>: <span class="string">'Besser'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Moe'</span>, <span class="attr">last</span>: <span class="string">'Howward'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Joe'</span>, <span class="attr">last</span>: <span class="string">'DeRita'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Shemp'</span>, <span class="attr">last</span>: <span class="string">'Howward'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Larry'</span>, <span class="attr">last</span>: <span class="string">'Fine'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Currly'</span>, <span class="attr">last</span>: <span class="string">'Howard'</span>&#125;</span><br><span class="line">];</span><br><span class="line">s.sort(by(<span class="string">'first'</span>));</span><br><span class="line"><span class="comment">/* s 是 </span></span><br><span class="line"><span class="comment">[</span></span><br><span class="line"><span class="comment">    &#123;first: 'Currly', last: 'Howard'&#125;</span></span><br><span class="line"><span class="comment">    &#123;first: 'Joe', last: 'Besser'&#125;,</span></span><br><span class="line"><span class="comment">    &#123;first: 'Joe', last: 'DeRita'&#125;,</span></span><br><span class="line"><span class="comment">    &#123;first: 'Larry', last: 'Fine'&#125;,</span></span><br><span class="line"><span class="comment">    &#123;first: 'Moe', last: 'Howward'&#125;,</span></span><br><span class="line"><span class="comment">    &#123;first: 'Shemp', last: 'Howward'&#125;,</span></span><br><span class="line"><span class="comment">];</span></span><br><span class="line"><span class="comment">*／</span></span><br></pre></td></tr></table></figure></p>
<p>基于多个键值排序，可以修改 by 函数，让其可以接受第二个参数，当主要的键值产生一个匹配的时候，另一个 compare 方法将被调用以决出高下。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// by 函数接受一个成员名字符串和一个可选的次要比较函数作为参数，</span></span><br><span class="line"><span class="comment">// 并返回一个可以用来对包含该成员的对象数组进行排序的比较函数。</span></span><br><span class="line"><span class="comment">// 当 o[name] === p[name] 时，次要比较函数被用来决出高下</span></span><br><span class="line"><span class="keyword">var</span> by = <span class="function"><span class="keyword">function</span>(<span class="params">name, minor</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span>(<span class="params">o, p</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> a, b;</span><br><span class="line">        <span class="keyword">if</span>(o &amp;&amp; p &amp;&amp; <span class="keyword">typeof</span> o === <span class="string">'object'</span> &amp;&amp; <span class="keyword">typeof</span> p === <span class="string">'object'</span>) &#123;</span><br><span class="line">            a = o[name];</span><br><span class="line">            b = p[name];</span><br><span class="line">            <span class="keyword">if</span>(a === b) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">typeof</span> minor === <span class="string">'function'</span> ? minor(o, p) : <span class="number">0</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">typeof</span> a === <span class="keyword">typeof</span> b) &#123;</span><br><span class="line">                <span class="keyword">return</span> a &lt; b ? <span class="number">-1</span> : <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">typeof</span> a &lt; <span class="keyword">typeof</span> b ? <span class="number">-1</span> : <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">throw</span> &#123;</span><br><span class="line">                name: <span class="string">'Error'</span>,</span><br><span class="line">                message: <span class="string">'Expected an object when sorting by '</span> + name</span><br><span class="line">            &#125;;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> s = [</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Joe'</span>, <span class="attr">last</span>: <span class="string">'Besser'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Moe'</span>, <span class="attr">last</span>: <span class="string">'Howward'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Joe'</span>, <span class="attr">last</span>: <span class="string">'DeRita'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Shemp'</span>, <span class="attr">last</span>: <span class="string">'Howward'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Larry'</span>, <span class="attr">last</span>: <span class="string">'Fine'</span>&#125;,</span><br><span class="line">    &#123;<span class="attr">first</span>: <span class="string">'Currly'</span>, <span class="attr">last</span>: <span class="string">'Howard'</span>&#125;</span><br><span class="line">];</span><br><span class="line">s.sort(by(<span class="string">'last'</span>, by(<span class="string">'first'</span>)));</span><br><span class="line"><span class="comment">/* s 是 </span></span><br><span class="line"><span class="comment">[</span></span><br><span class="line"><span class="comment">    &#123;first: 'Joe', last: 'Besser'&#125;,</span></span><br><span class="line"><span class="comment">    &#123;first: 'Joe', last: 'DeRita'&#125;,</span></span><br><span class="line"><span class="comment">    &#123;first: 'Larry', last: 'Fine'&#125;,</span></span><br><span class="line"><span class="comment">    &#123;first: 'Currly', last: 'Howard'&#125;</span></span><br><span class="line"><span class="comment">    &#123;first: 'Moe', last: 'Howward'&#125;,</span></span><br><span class="line"><span class="comment">    &#123;first: 'Shemp', last: 'Howward'&#125;,</span></span><br><span class="line"><span class="comment">];</span></span><br><span class="line"><span class="comment">*／</span></span><br></pre></td></tr></table></figure></p>
<h3 id="array-splice-start-deleteCount-item…"><a href="#array-splice-start-deleteCount-item…" class="headerlink" title="array.splice(start, deleteCount, item…)"></a>array.splice(start, deleteCount, item…)</h3><p>splice 方法从 array 中移除 1 个或多个元素，并用新的 item 替换它们。参数 start 是从数组 array 中移除元素的开始位置。参数 deleteCount 是要移除元素的个数。如果有额外的参数那些 item 都将插入到所移除元素的位置上它返回一个包含被移除元素的数组。<br>splice 最主要的用处是从一个数组中删除元素。千万不要与 slice 混淆，slice 主要的用处是对数组的一段做浅复制。</p>
<h3 id="array-unshift-item…"><a href="#array-unshift-item…" class="headerlink" title="array.unshift(item…)"></a>array.unshift(item…)</h3><p>unshift 方法像 push 方法一样用于将元素添加到数组中，但它是把元素插入到 array 的开始部分而不是尾部。它返回 array 的新的长度值。</p>
<h2 id="Function"><a href="#Function" class="headerlink" title="Function"></a>Function</h2><h3 id="function-apply-thisArg-argArray"><a href="#function-apply-thisArg-argArray" class="headerlink" title="function.apply(thisArg, argArray)"></a>function.apply(thisArg, argArray)</h3><p>apply 方法调用函数 function，传递一个将被绑定到 this 的对象和一个可选的参数数组。apply 方法被用在apply调用模式中。</p>
<h2 id="Number"><a href="#Number" class="headerlink" title="Number"></a>Number</h2><h3 id="number-toExponential-fractionDigits"><a href="#number-toExponential-fractionDigits" class="headerlink" title="number.toExponential(fractionDigits)"></a>number.toExponential(fractionDigits)</h3><p>toExponential 方法把这个 number 转换成一个指数形式的字符串。可选参数 fractionDigits 控制其小数点后的数字位数。它的值必须在 0 至 20 之间。</p>
<h3 id="number-toFixed-fractionDigits"><a href="#number-toFixed-fractionDigits" class="headerlink" title="number.toFixed(fractionDigits)"></a>number.toFixed(fractionDigits)</h3><p>toFixed 方法把这个 number 转换成为一个十进制数形式的字符串。可选参数 fractionDigits 控制其小数点后的数字位数。它的值必须在 0 至 20 之间。默认为 0.</p>
<h3 id="number-toPrecision-precision"><a href="#number-toPrecision-precision" class="headerlink" title="number.toPrecision(precision)"></a>number.toPrecision(precision)</h3><p>toPrecision 方法把这个 number 转换成为一个十进制数形式的字符串。可选参数 precision 控制有效数字的位数。它的值必须在 0 至 21 之间。</p>
<h3 id="number-toString-radix"><a href="#number-toString-radix" class="headerlink" title="number.toString(radix)"></a>number.toString(radix)</h3><p>toString 方法把这个 number 转换成为一个字符串。可选参数 radix 控制基数。它的值必须在 2 和 36 之间。默认的 radix 是以 10 为基数的。radix 参数最常用的是整数，但它可以用任意的数字。</p>
<h2 id="Object"><a href="#Object" class="headerlink" title="Object"></a>Object</h2><h3 id="object-hadOwnProperty-name"><a href="#object-hadOwnProperty-name" class="headerlink" title="object.hadOwnProperty(name)"></a>object.hadOwnProperty(name)</h3><p>如果这个 object 包含了一个名为 name 的属性，那么这个 hasOwnProperty 方法返回 true。原型链中的同名属性是不会被检查的。</p>
<h2 id="RegExp"><a href="#RegExp" class="headerlink" title="RegExp"></a>RegExp</h2><h3 id="regexp-exec-string"><a href="#regexp-exec-string" class="headerlink" title="regexp.exec(string)"></a>regexp.exec(string)</h3><p>exec 方法是使用正则表达式的最强大（和最慢）的方法。如果它成功地匹配 regexp 和字符串 string，它会返回一个数组。数组中下标为 0 的元素将包含正则表达式 regexp 匹配的子字符串。下标为 1 的元素是分组 1 捕获的文本，下标为 2 元素是分组 2 捕获的文本，以此类推。如果匹配失败，那么它会返回 null。   </p>
<p>如果 regexp 带有一个 g 标志（全剧标志），事情变得有点更加复杂了。查找不是从这个字符串的起始位置开始，而是从 regexp.lastIndex（它初始化为 0）位置开始。如果匹配成功，那么 regexp.lastIndex将被设置为该匹配后第一个字符串的位置。不成功的匹配会重置 regexp.lastIndex为 0。   </p>
<p>这就允许你通过在一个循环中调用 exec 去查询一个匹配模式在一个字符串中发生几次。有两件事情需要注意。如果你提前退出了这个循环，再次进入这个循环前必须把 regexp.lastIndex 重置到 0。^ 因子也仅匹配 regexp.lastIndex为 0的情况。</p>
<h3 id="regexp-test-string"><a href="#regexp-test-string" class="headerlink" title="regexp.test(string)"></a>regexp.test(string)</h3><p>test 方法是使用正则表达式的最简单（和最快）的方法。如果该 regexp 匹配 string，它返回 true，否则，返回 false。不要对这个方法使用 g 标识。</p>
<h2 id="String"><a href="#String" class="headerlink" title="String"></a>String</h2><h3 id="string-charAt-pos"><a href="#string-charAt-pos" class="headerlink" title="string.charAt(pos)"></a>string.charAt(pos)</h3><p>charAt 方法返回在 string 中 pos 位置处的字符串。如果 pos 小于 0 或大于等于字符串的长度 string.length，它会返回空字符串。JavaScript 没有字符类型。这个方法返回的结果是一个字符串。</p>
<h3 id="string-charCodeAt-pos"><a href="#string-charCodeAt-pos" class="headerlink" title="string.charCodeAt(pos)"></a>string.charCodeAt(pos)</h3><p>charCodeAt 方法同 charAt 一样，只不过它返回的不是一个字符串，而是以整数形式表示的在 string 中的 pos 位置处的字符串的字符码位。</p>
<h3 id="string-concat-string…"><a href="#string-concat-string…" class="headerlink" title="string.concat(string…)"></a>string.concat(string…)</h3><p>concat 方法通过将其他的字符串连接在一起来构造一个新的字符串。它很少被使用，因为 + 运算符更为方便。</p>
<h3 id="string-indexOf-searchString-position"><a href="#string-indexOf-searchString-position" class="headerlink" title="string.indexOf(searchString, position)"></a>string.indexOf(searchString, position)</h3><p>concat 方法在 string 内查找另一个字符串 searchString。如果它被找到，则返回第一个匹配字符的位置，否则返回 -1。可选参数 position 可设置从 string 的某个指定的位置开始查找。</p>
<h3 id="string-lastIndexOf-searchString-position"><a href="#string-lastIndexOf-searchString-position" class="headerlink" title="string.lastIndexOf(searchString, position)"></a>string.lastIndexOf(searchString, position)</h3><p>lastIndexOf 方法和 indexOf 方法类似，只不过它是从该字符串的末尾开始查找而不是从开头。</p>
<h3 id="string-localeCompare-that"><a href="#string-localeCompare-that" class="headerlink" title="string.localeCompare(that)"></a>string.localeCompare(that)</h3><p>localeCompare 方法比较两个字符串。如何比较字符串的规则没有详细说明。如果 string 比字符串 that 小，那么结果为负数。如果它们是相等的，那么结果为0。这类似于 array.sort比较函数的约定。</p>
<p>未完。。。</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/02/04/动态规划/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/02/04/动态规划/" itemprop="url">
                  动态规划
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-02-04T16:51:38+08:00">
                2017-02-04
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="动态规划算法的思路以及实现"><a href="#动态规划算法的思路以及实现" class="headerlink" title="动态规划算法的思路以及实现"></a>动态规划算法的思路以及实现</h2><h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><blockquote>
<p>动态规划(DP)是算法设计思想当中最难也是最有趣的部分了，动态规划适用于有重叠子问题和最优子结构性质的问题，是一种在数学、计算机科学和经济学中经常使用的，通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法。使用动态规划方法解题有较高的时间效率，关键在于它减少了很多不必要的计算和重复计算的部分。</p>
</blockquote>
<p>它的思想就是把一个大的问题进行拆分，细分成一个个小的子问题，且能够从这些小的子问题的解当中推导出原问题的解。同时还需要满足以下两个重要的性质才能进行动态规划：</p>
<ul>
<li>最优子结构性：既所拆分的子问题的解是最优解。</li>
<li>子问题重叠性质：既在求解的过程当中，每次产生的子问题并不总是新问题，有些子问题会被重复计算多次。动态规划算法是利用了这种子问题的重叠性质，对每一个子问题只计算一次，然后将其计算结果保存在一个表格中，当再次需要计算已经计算过的子问题时，只是在表格中简单地查看一下结果，从而获得较高的解题效率。</li>
</ul>
<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><blockquote>
<p>首先引用一道动态规划的经典问题<strong>最长不下降子序列</strong><br>它的定义是：设有由n个不相同的整数组成的数列 b[n]，若有下标 i1 &lt; i2 &lt; … &lt; iL 且 b[i1] &lt; b[i2] &lt; … &lt; b[iL]<br>则称存在一个长度为 L 的不下降序列。</p>
</blockquote>
<h3 id="例如"><a href="#例如" class="headerlink" title="例如"></a>例如</h3><blockquote>
<p>13, 7, 9, 16, 38, 24, 37, 18, 44, 19, 21, 22, 63, 15</p>
</blockquote>
<p>那么就有 13 &lt; 16 &lt; 38 &lt; 44 &lt; 63 长度为5的不下降子序列。<br>但是经过观察实际上还有 7 &lt; 9 &lt; 16 &lt; 18 &lt; 19 &lt; 21 &lt; 22 &lt; 63 长度为8的不下降子序列，那么是否还有更长的不下降子序列呢？请找出最长的不下降子序列。</p>
<h3 id="输入格式"><a href="#输入格式" class="headerlink" title="输入格式"></a>输入格式</h3><blockquote>
<p>第一行为 n，表示 n 个数(n &lt;= 100000)，第二行为 n 个数的数值(数字之间用空格隔开且最后一个数字末尾不能留有空格)。</p>
</blockquote>
<h3 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h3><blockquote>
<p>一个整数，表示最长不下降序列的长度。</p>
</blockquote>
<h3 id="输入例子"><a href="#输入例子" class="headerlink" title="输入例子"></a>输入例子</h3><blockquote>
<p>4<br>1 3 1 2</p>
</blockquote>
<h3 id="输出例子"><a href="#输出例子" class="headerlink" title="输出例子"></a>输出例子</h3><blockquote>
<p>2</p>
</blockquote>
<h3 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h3><blockquote>
<p>假如要求得某一段的最优，就要想更小段的最优怎么求，再看看由最小段的最优能否扩大推广到最大段的最优。所以该问题存在最优子结构，而从小段的最优子结构到更大的最优子结构，所有子结构的求解问题是相同的，即满足动态规划的性质。</p>
</blockquote>
<p>假设这么一个表：   </p>
<table>
<thead>
<tr>
<th>序列下标</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
</tr>
</thead>
<tbody>
<tr>
<td>序列数值</td>
<td>13</td>
<td>7</td>
<td>9</td>
<td>16</td>
<td>38</td>
<td>24</td>
<td>37</td>
<td>18</td>
<td>44</td>
<td>19</td>
<td>21</td>
<td>22</td>
<td>63</td>
<td>15</td>
</tr>
<tr>
<td>序列长度</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>链接位置</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
<td>-1</td>
</tr>
</tbody>
</table>
<blockquote>
<p>第三行表示该序列元素的所能连接的最长不下降子序列的长度，因为本身长度为1，所以初始值都为1。<br>第四行表示链接于哪个序列元素形成最长不下降子序列。</p>
</blockquote>
<h4 id="1-从后向前"><a href="#1-从后向前" class="headerlink" title="1.从后向前"></a>1.从后向前</h4><p>先从倒数第二项 63 算起，在它的后面仅有一项，因此仅作一次比较，因为 63 &gt; 15，所以从 63 出发，不作任何链接，长度还是为1。   </p>
<p>再看倒数第三项 22，在它的后面有 2 项，因此必须要在这 2 项当中找出比 22 大，长度又是最长的数值作为链接，由于只有 22 &lt; 63，所以修改 22 的长度为 2，即自身长度加上所链接数值的长度，并修改链接位置为 13，也就是 63 的下标。   </p>
<p>再看倒数第四项 21，在它的后面有 3 项，因此必须要在这3项当中找出比 21 大，长度又是最长的数值作为链接(注意:是长度)，很容易看出，数值 22 满足该条件，因此，修改 21 的长度为3，并修改链接位置为 12，即 22 的序列下标。   </p>
<p>依次类推，最后结果如下表：   </p>
<table>
<thead>
<tr>
<th>序列下标</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
</tr>
</thead>
<tbody>
<tr>
<td>序列数值</td>
<td>13</td>
<td>7</td>
<td>9</td>
<td>16</td>
<td>38</td>
<td>24</td>
<td>37</td>
<td>18</td>
<td>44</td>
<td>19</td>
<td>21</td>
<td>22</td>
<td>63</td>
<td>15</td>
</tr>
<tr>
<td>序列长度</td>
<td>7</td>
<td>8</td>
<td>7</td>
<td>6</td>
<td>3</td>
<td>4</td>
<td>3</td>
<td>5</td>
<td>2</td>
<td>4</td>
<td>3</td>
<td>2</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>链接位置</td>
<td>3</td>
<td>2</td>
<td>3</td>
<td>7</td>
<td>8</td>
<td>6</td>
<td>8</td>
<td>9</td>
<td>12</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>-1</td>
<td>-1</td>
</tr>
</tbody>
</table>
<blockquote>
<p>最终状态的转移方程为：f(i) = maxf(j) + 1 (bj &gt; bi 且 i &lt; j)，时间复杂度为 O(n^2)</p>
</blockquote>
<h4 id="2-从前向后"><a href="#2-从前向后" class="headerlink" title="2.从前向后"></a>2.从前向后</h4><table>
<thead>
<tr>
<th>序列下标</th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
</tr>
</thead>
<tbody>
<tr>
<td>序列数值</td>
<td>13</td>
<td>7</td>
<td>9</td>
<td>16</td>
<td>38</td>
<td>24</td>
<td>37</td>
<td>18</td>
<td>44</td>
<td>19</td>
<td>21</td>
<td>22</td>
<td>63</td>
<td>15</td>
</tr>
<tr>
<td>序列长度</td>
<td>1</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>4</td>
<td>5</td>
<td>4</td>
<td>6</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>3</td>
</tr>
<tr>
<td>链接位置</td>
<td>-1</td>
<td>-1</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>3</td>
<td>5</td>
<td>3</td>
<td>6</td>
<td>7</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>2</td>
</tr>
</tbody>
</table>
<blockquote>
<p>最终状态的转移方程为：f(i) = maxf(j) + 1 (bj &lt; bi 且 i &gt; j)，时间复杂度为 O(n^2)</p>
</blockquote>
<h3 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">process.stdin.setEncoding(<span class="string">'utf8'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> arr = [], <span class="comment">// 接收输入参数的数组</span></span><br><span class="line">    bool = <span class="number">0</span>, <span class="comment">// 判断是否满足输入条件</span></span><br><span class="line">    n = <span class="number">0</span>, <span class="comment">// 数列元素个数</span></span><br><span class="line">    longest = <span class="number">1</span>, <span class="comment">// 最长不下降子序列长度</span></span><br><span class="line">    a = [], <span class="comment">// 数列元素数组</span></span><br><span class="line">    dp = []; <span class="comment">// 动态规划过程中子序列长度数组</span></span><br><span class="line"></span><br><span class="line">process.stdin.on(<span class="string">'readable'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> chunk = process.stdin.read();</span><br><span class="line">    <span class="keyword">if</span>(chunk !== <span class="literal">null</span>) &#123;</span><br><span class="line">        arr.push(chunk.trim());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span>(bool &gt;= <span class="number">2</span>) &#123;</span><br><span class="line">        n = <span class="built_in">parseInt</span>(arr[<span class="number">0</span>]);</span><br><span class="line">        process.stdin.emit(<span class="string">'end'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    bool++;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">process.stdin.on(<span class="string">'end'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    a = arr.slice(<span class="number">1</span>).join(<span class="string">" "</span>).split(<span class="string">" "</span>).map(<span class="function"><span class="keyword">function</span>(<span class="params">index, elem</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">parseInt</span>(index);</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">if</span>(n !== a.length) &#123;</span><br><span class="line">        process.stdout.write(<span class="string">'长度不一致'</span>);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; n; i++) &#123;</span><br><span class="line">        seq[i] = <span class="number">-1</span>;</span><br><span class="line">        dp[i] = <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; i; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span>(a[i] &gt; a[j]) &#123;</span><br><span class="line">                dp[i] = <span class="built_in">Math</span>.max(dp[j] + <span class="number">1</span>, dp[i]);</span><br><span class="line">                (<span class="function"><span class="keyword">function</span>(<span class="params">index, arg</span>) </span>&#123;</span><br><span class="line">                    seq[index] = arg;                    </span><br><span class="line">                &#125;)(i, j);</span><br><span class="line">            &#125;</span><br><span class="line">            longest = <span class="built_in">Math</span>.max(dp[i], longest);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`最长长度为：<span class="subst">$&#123;longest&#125;</span>`</span>);</span><br><span class="line"></span><br><span class="line">    process.stdout.write(<span class="string">'end'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="输入输出"><a href="#输入输出" class="headerlink" title="输入输出"></a>输入输出</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">14</span><br><span class="line">13 7 9 16 38 24 37 18 44 19 21 22 63 15</span><br><span class="line">最长长度为：8</span><br><span class="line">end</span><br></pre></td></tr></table></figure>
          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/02/03/SSL-TLS/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/02/03/SSL-TLS/" itemprop="url">
                  SSL/TLS
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-02-03T16:27:50+08:00">
                2017-02-03
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h2 id="SSL-TLS协议运行机制"><a href="#SSL-TLS协议运行机制" class="headerlink" title="SSL/TLS协议运行机制"></a>SSL/TLS协议运行机制</h2><hr>
<p>互联网的通信安全，建立在 SSL/TLS 协议之上。<br>本文简要介绍 SSL/TLS 协议的运行机制。   </p>
<h3 id="一-作用"><a href="#一-作用" class="headerlink" title="一.作用"></a>一.作用</h3><p>不使用 SSL/TLS 的 HTTP 通信，就是不加密的通信。所有信息明文传播，带来了三大风险：</p>
<pre>
1.窃听风险 
2.篡改风险
3.冒充风险
</pre>

<p>SSL/TLS 协议就是为了解决这三大风险而设计的，希望达到：</p>
<pre>
1.所有信息都是加密传播，第三方无法窃听。
2.具有校验机制，一旦被篡改，通信双方会立刻发现。
3.具有身份证书，防止身份被冒充。
</pre>

<p>互联网是开放环境，通信双方都是未知身份，这为协议的设计带来了很大的难度。而且，协议还必须能够经受所有匪夷所思的攻击，这使得 SSL/TLS 协议变得异常复杂。</p>
<h3 id="二-历史"><a href="#二-历史" class="headerlink" title="二.历史"></a>二.历史</h3><p>互联网加密通信协议的历史，几乎与互联网一样长。</p>
<pre>
1994年，NetScape 公司设计了 SSL 协议1.0版本，但是未发布。
1995年，NetScape 公司发布了 SSL 2.0版，但很快发现有严重漏洞。
1996年，SSL 3.0 版问世，得到大规模应用。
1999年，互联网标准化组织 ISOC 接替 NetScape 公司，发布了 SSL 的升级版 TLS 1.0版。
2006年和2008年，TLS 进行了两次升级，分别是 TLS 1.1版和 TLS 1.2版。
</pre>

<p>目前，应用最广泛的是 TLS 1.0，接下来是 SSL 3.0。但是，主流浏览器都实现了 TLS 1.2的支持。<br>TLS 1.0 通常被标示为 SSL 3.1，TLS 1.1 为 SSL 3.2，TLS 1.2 为 SSL 3.3。</p>
<h3 id="三-基本的运行过程"><a href="#三-基本的运行过程" class="headerlink" title="三.基本的运行过程"></a>三.基本的运行过程</h3><p>SSL/TLS 协议的基本思路是采用公钥加密法，也就是说客户端先向服务器端索要公钥，然后用公钥加密信息，服务器接收密文后，用自己的私钥解密。<br>但是，这里有两个问题：</p>
<pre>
1.如何保证公钥不被篡改？
解决方法：将公钥放在数字证书中。只要证书是可信的，公钥就是可信的。

2.公钥加密计算量太大，如何减少耗用的时间？
解决方法：每一次对话（session），客户端和服务器端都生成一个“对话密钥”（session key），用它来加密信息。
由于“对话密钥”是对称加密，所以运算速度非常快，而服务器公钥只用于加密“对话密钥”本身，这样就减少了加密运
算的消耗时间。
</pre>

<p>因此，SSL/TLS协议的基本过程是这样的：</p>
<pre>
1.客户端向服务器端所要并验证公钥。
2.双方协商生成“对话密钥”。
3.双方采用“对话密钥”进行加密通信。
</pre>

<p>上面过程的前两步，又称为“握手阶段”。</p>
<h3 id="四-握手阶段的详细过程"><a href="#四-握手阶段的详细过程" class="headerlink" title="四.握手阶段的详细过程"></a>四.握手阶段的详细过程</h3><p>“握手阶段”设计四次通信，我们一个个来看。需要注意的是，“握手阶段”的所有通信都是明文的。</p>
<h4 id="4-1客户端发出请求"><a href="#4-1客户端发出请求" class="headerlink" title="4.1客户端发出请求"></a>4.1客户端发出请求</h4><p>首先，客户端（通常是浏览器）先向服务器发出加密通信的请求，这被叫做 ClientHello 请求。<br>在这一步，客户端主要向服务器提供一下信息：</p>
<pre>
1.支持的协议的版本。
2.一个客户端生成的随机数，稍后用于生成”对话密钥“。
3.支持的加密方法。
4.支持的压缩方法。
</pre>

<p>这里需要注意的是，客户端发送的信息之中不包括服务器的域名。也就是说，理论上服务器只能包含一个网站，否则<br>会分不清应该向客户端提供哪一个网站的数字证书。这就是为什么通常一台服务器只能有一张数字证书的原因。   </p>
<p>对于虚拟主机用户来说，这当然不方便。2006年，TLS协议加入了一个 Server Name Indication 扩展，允许客户<br>端向服务器提供它请求的域名。   </p>
<h4 id="4-2服务器回应"><a href="#4-2服务器回应" class="headerlink" title="4.2服务器回应"></a>4.2服务器回应</h4><p>服务器收到客户端请求后，向客户端发出回应，这叫做 ServerHello。服务器的回应包含以下内容。</p>
<pre>
1.确认使用的加密通信协议版本。如果浏览器与服务器支持的版本不一致，服务器关闭加密通信。
2.一个服务器生成的随机数，稍后用于生成”对话密钥“。
3.确认使用的加密方法。
4.服务器证书。
</pre>

<p>除了上面这些信息，如果服务器需要确认客户端的身份，就会再包含一项请求，要求客户端提供”客户端证书“。比如，金融机构往往只允许认证客户端连入自己的网络，就会向正式客户提供USB密钥，里面包含了一张客户端证书。   </p>
<h4 id="4-3客户端回应"><a href="#4-3客户端回应" class="headerlink" title="4.3客户端回应"></a>4.3客户端回应</h4><p>客户端收到服务器回应后，首先验证服务器证书。如果证书不是可信机构颁布，或者证书中的域名与实际域名不一致，或者证书已经过期，就会向访问者显示一个警告，由其选择是否还要继续通信。   </p>
<p>如果证书没有问题，客户端就会从证书中取出服务器的公钥。然后，向服务器发送下面三项信息。</p>
<pre>
1.一个随机数。该随机数用于服务器公钥加密，防止被窃听。
2.编码改变通知，表示随后的信息都用双方商定的加密方法和密钥发送。
3.客户端握手结束通知，表示客户端的握手阶段已经结束。这一项同时也是前面发送的所有内容的 hash 值，用来
供服务器校验。
</pre>

<p>上面第一项的随机数，是整个握手阶段出现的第三个随机数，又称”pre-master key”。有了它以后，客户端和服务器就同时有了三个随机数，接着双方就用事先商定的加密方法，各自生成本次会话所用的同一把”会话密钥”。   </p>
<p>至于为什么一定要用三个随机数，来生成”会话密钥”，解释：   </p>
<blockquote>
<p>“不管是客户端还是服务器，都需要随机数，这样生成的密钥才不会每次都一样。由于SSL协议中证书是静态的，因此十分有必要引入一种随机因素来保证协商出来的密钥的随机性。<br>对于 RSA 密钥交换算法来说，pre-master-key 本身就是一个随机数，再加上 hello 消息中的随机，三个随机数通过一个密钥导出器最终导出一个对称密钥。<br>pre master 的存在在于 SSL 协议不信任每个主机都能产生完全随机的随机数，如果随机数不随机，那么 pre master secret 就有可能被猜出来，那么仅适用 pre master secret 作为密钥就不合适了，因此必须引入新的随机因素，那么客户端和服务器加上 pre master secret 三个随机数一同生成的密钥就不容易被猜出了，一个伪随机可能完全不随机，可是是三个伪随机就十分接近随机了，每增加一个自由度，随机性增加的可不是一。”</p>
</blockquote>
<p>此外，如果前一步，服务器要求客户端证书，客户端会在这一步发送证书及相关信息。   </p>
<h4 id="4-4-服务器的最后回应"><a href="#4-4-服务器的最后回应" class="headerlink" title="4.4 服务器的最后回应"></a>4.4 服务器的最后回应</h4><p>服务器收到客户端的第三个随机数 pre-master key 之后，计算生成本次会话所用的”会话密钥”。然后，向客户端最后发送下面信息。</p>
<pre>
1.编码改变通知，表示随后的信息都将用双方商定的加密方法和密钥发送。
2.服务器握手结束通知，表示服务器的握手阶段已经结束。这一项同时也是前面发送的所有内容的 hash 值，用来
供客户端校验。
</pre>

<p>至此，整个握手阶段全部结束。接下来，客户端与服务器进入加密通信，就完全是使用普通的 HTTP 协议，只不过用”会话密钥”加密内容。</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/01/23/search-skill/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2017/01/23/search-skill/" itemprop="url">
                  Google 搜索引擎技巧
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-01-23T22:36:40+08:00">
                2017-01-23
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="Google-搜索引擎技巧"><a href="#Google-搜索引擎技巧" class="headerlink" title="Google 搜索引擎技巧"></a>Google 搜索引擎技巧</h1><p>1.双引号<br>把搜索词放在双引号中代表完全匹配搜索。</p>
<p>2.减号<br>减号代表搜索不包含减号后面的词，使用这个指令减号前面必须是空格，减号后面没有空格。</p>
<p>3.星号<br>通配符</p>
<p>4.~<br>同时搜索近义词</p>
<p>5.inurl<br>用于搜索查询词出现在url中。支持中文。e.g. inurl:markdown</p>
<p>6.inanchor<br>用于搜索链接锚文字包含搜索词。</p>
<p>7.intitle<br>用于搜索页面title中包含关键词</p>
<p>8.alinurl<br>用于搜索多组查询词出现在url中</p>
<p>9.alintitle<br>用于搜索页面标题中包含多组关键词</p>
<p>10.filetype<br>用于搜索特定文件格式</p>
<p>11.site<br>用来搜索某个域名下的所有文件</p>
<p>12.linkdomain<br>只适用于雅虎，返回的是某个域名的反向链接。</p>
<p>13.related<br>返回的结果是与某个网站有关联的页面。</p>
<p>14.year..year<br>时间段。e.g. 2008..2010，搜索2008年至2010年之间的结果</p>
<p>15.define<br>快速查询字词定义。e.g. define:angary，查询angary的定义</p>
<p>e.g.<br>. inurl:gov 减肥<br>返回的是url中包含gov，页面中有“减肥”这个词的页面</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/05/14/团队中的git实践/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="Ji Xiang">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://o7bp9e1ec.bkt.clouddn.com/1485173143816">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="Ji Xiang's blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="Ji Xiang's blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                
                <a class="post-title-link" href="/2016/05/14/团队中的git实践/" itemprop="url">
                  团队中的git实践
                </a>
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-05-14T20:50:07+08:00">
                2016-05-14
              </time>
            

            

            
          </span>

          

          
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p>为了获得软件质量和开发效率的双重收益，软件项目开发早已不再是一个人的事情了，而是一个团队(team)的事情。不但如此，现今的软件开发规模越来越大，所涉及的技术门类也越来越复杂，一个人难以独挑大梁，同时，一个人也难以在短周期内完成复杂项目。团队的意义不仅在于提高开发效率，更能保证开发质量，一人负责一块开发内容就可以用充足的精力和热情使这块内容做的更加完美。<br>说到团队开发，总是离不开项目管理和版本控制工具，这里我们将会讨论版本控制工具——git在团队中正确实践的姿势。<br>Git/Github是任何一个非小白程序员都知道或使用的工具/网站，随着软件开发学龄的深入，我自己的体会是越来越离不开这个东西了。一个人用git可以用得随心所欲，但团队中就是另外一回事了。一个人的项目，代码怎么改、怎么提交都不会有冲突，这样也会养成一些不良习惯，把这些习惯带到团队开发中，轻则让队友讨厌，重则酿成大祸（这不是危言耸听）。   </p>
<h3 id="1-习惯养成"><a href="#1-习惯养成" class="headerlink" title="1.习惯养成"></a>1.习惯养成</h3><p>如果一个团队在使用git时没有一些规范，那将是一场难以醒来的噩梦！然而，规范固然重要，但更重要的是个人素质，在使用git时需要自己养成良好的习惯。</p>
<h3 id="2-提交"><a href="#2-提交" class="headerlink" title="2.提交"></a>2.提交</h3><p>Commit Message格式   </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&lt;type&gt;(&lt;scope&gt;): &lt;subject&gt;</span><br><span class="line">&lt;space&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;space&gt;</span><br><span class="line">&lt;footer&gt;</span><br></pre></td></tr></table></figure>
<p>上面是一次提交后Message格式规范，分成标题、内容详情、结尾三个部分，各有各的用处。<br>头部即首行，是可以直接在页面中预览的部分，一共有三个部分<type>, <scope>, <subject>, 含义分别如下：</subject></scope></type></p>
<p><em>Type</em>   </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">- feat: 新功能(feature)</span><br><span class="line">- fix: 修补bug</span><br><span class="line">- docs: 文档(documentation)</span><br><span class="line">- style: 格式(不影响代码运行的变动)</span><br><span class="line">- refactor: 重构(即不是新增功能，也不是修改bug的代码变动)</span><br><span class="line">- test: 增加测试</span><br><span class="line">- chore: 构建过程或辅助工具的变动</span><br></pre></td></tr></table></figure>
<p><em>Scope</em>   </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">用来说明本次提交影响的范围，即简要说明修改会涉及的部分。</span><br></pre></td></tr></table></figure>
<p><em>Subject</em></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">用来简要描述本次改动，概述就好了，因为后面还会在Body里给出具体信息。并且最好遵循</span><br><span class="line">下面三条：</span><br><span class="line"> - 以动词开头，使用第一人称现在时</span><br><span class="line"> - 首字母不要大写</span><br><span class="line"> - 结尾不用句号( . )</span><br></pre></td></tr></table></figure>
<p><em>Body</em>   </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;里的内容是对上面subject里内容的展开，在此作更加详尽的描述，内容里应该包括修改动机和修改前后的对比。</span><br></pre></td></tr></table></figure>
<p><em>Footer</em>   </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;footer&gt;里主要放置不兼容变更和Issue关闭的信息。</span><br></pre></td></tr></table></figure>
<h4 id="Revert"><a href="#Revert" class="headerlink" title="Revert"></a>Revert</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">此外如果需要撤销之前的提交，那么本次提交Message中必须以revert: 开头，后面紧跟前面描述的Header部分，格式不变。</span><br></pre></td></tr></table></figure>
<p>在具体开发工作中主要需要遵守的原则就是[使每次提交都有质量]，只要坚持做到以下几点：</p>
<ol>
<li>提交时的粒度是一个小功能点或者一个bug fix，这样进行恢复等操作时能够将[误伤]减到最低；</li>
<li>用一句简练的话写在第一行，然后空一行稍微详细阐述提交所增加或修改的地方；</li>
<li>不要没提交一次就推送一次，多积攒几个提交后一次性推送，这样可以避免在进行一次提交后发现代码中还有小错误。</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">加入代码已经提交了，对这次提交的内容进行检查时发现里面有个变量单次拼错了或者其他失误，只要还没有推送到远程，</span><br><span class="line">就有一个不被他人发觉你的疏忽的补救方法——   </span><br><span class="line">首先，把失误修正之后提交，可以用与上次提交相同的Message。   </span><br><span class="line">然后，终端中执行&lt;code&gt;git rebase -i [SHA]&lt;/code&gt;，其中SHA是上一次提交之前的那次提交的。   </span><br><span class="line">最后，这样就将两次提交的节点合并成一个，甚至能够修改提交信息。</span><br></pre></td></tr></table></figure>
<h3 id="3-推送"><a href="#3-推送" class="headerlink" title="3.推送"></a>3.推送</h3><p>当自己一个人进行开发时，在功能完成之前不要急着创建远程分支。   </p>
<h3 id="4-拉取和合并"><a href="#4-拉取和合并" class="headerlink" title="4.拉取和合并"></a>4.拉取和合并</h3><p>在将其他分支的代码合并到当前分支时，如果那个分支是当前分支的父分支，为了保持图表的可读性和可追踪性，<br>可以考虑用 git rebase 来代替 git merge；反过来或者不是父子关系的两个分支以及互相已经 git merge 过的分支，<br>就不要采用 git rebase 了，避免出现重复的冲突和提交节点。   </p>
<h3 id="最后献上一句话"><a href="#最后献上一句话" class="headerlink" title="最后献上一句话"></a>最后献上一句话</h3><p>日拱一卒，功不唐捐。</p>

          
        
      
    </div>

    <div>
      
    </div>

    <div>
      
    </div>


    <footer class="post-footer">
      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>


          
          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://o7bp9e1ec.bkt.clouddn.com/1485173143816"
               alt="Ji Xiang" />
          <p class="site-author-name" itemprop="name">Ji Xiang</p>
          <p class="site-description motion-element" itemprop="description"></p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">posts</span>
              </a>
            </div>
          

          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">14</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Ji Xiang</span>
</div>


<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  




	





  





  

  




  
  

  

  

  

  


</body>
</html>
